# Maintainer: Biswapriyo Nath <nathbappai@gmail.com>

_realname=spirv-cross
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
epoch=1
pkgver=1.3.261.0
pkgrel=1
pkgdesc="A tool and library for parsing and converting SPIR-V to other shader languages (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url='https://github.com/KhronosGroup/SPIRV-Cross/'
license=('spdx:Apache-2.0')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-glslang"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-python"
             "${MINGW_PACKAGE_PREFIX}-spirv-headers"
             "${MINGW_PACKAGE_PREFIX}-spirv-tools"
             "${MINGW_PACKAGE_PREFIX}-cc")
source=(https://github.com/KhronosGroup/SPIRV-Cross/archive/refs/tags/sdk-${pkgver}.tar.gz)
sha256sums=('09f779bcac08613cb13d14fd17eb3fc2ea165bca7eefb84a7c9dee6fb29ba492')

prepare() {
  cd "${srcdir}/SPIRV-Cross-sdk-${pkgver}"

  # Use system provided glslang and spirv-tools binaries.
  sed -e "s|NO_DEFAULT_PATH||g" -i CMakeLists.txt
}

build() {
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G "Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    "${_extra_config[@]}" \
    -DSPIRV_CROSS_SHARED=ON \
    -DSPIRV_CROSS_ENABLE_TESTS=OFF \
    "../SPIRV-Cross-sdk-${pkgver}"

  ${MINGW_PREFIX}/bin/cmake.exe --build ./
}

check() {
  cd "${srcdir}/build-${MSYSTEM}"
  ${MINGW_PREFIX}/bin/cmake -DSPIRV_CROSS_ENABLE_TESTS=ON ../SPIRV-Cross-sdk-${pkgver}
  ${MINGW_PREFIX}/bin/cmake --build .
  ${MINGW_PREFIX}/bin/ctest.exe ./ || warning "Tests failed"
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --install .

  install -Dm644 "${srcdir}/SPIRV-Cross-sdk-${pkgver}/LICENSE" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
